﻿using GRPCClass;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SdtpsNodeLibrary
{
    /// <summary>
    /// 任务执行节点任务执行
    /// </summary>
    public class TaskNodeHandle
    {
        //public static SdtpsBase.TaskRequestCollection taskRequestList = new SdtpsBase.TaskRequestCollection();

        public static TaskReplyCollection taskReplyList = new TaskReplyCollection();
        /// <summary>
        /// 正在执行的列表
        /// </summary>
        public static TaskRequestCollection taskReplyRuning = new TaskRequestCollection();
        /// <summary>
        /// 反馈中心节点失败的队列
        /// </summary>
        public static Queue<TaskReply> taskReplyErrs = new Queue<TaskReply>();
        /// <summary>
        ///  任务执行队列
        /// </summary>
        public static Queue<TaskRequest> TaskRequestList = new Queue<TaskRequest>();

        /// <summary>
        /// 任务分配
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static int AllotTask(TaskRequest request)
        {
            TaskRequestList.Enqueue(request);
            return 1;

        }
        /// <summary>
        /// 查询任务执行的情况
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static TaskReply QueryTask(TaskRequest request)
        {
            TaskReply v= taskReplyList.findTaskReply(request.ID);
            if (v != null)
            {
                return v;
            }
            TaskRequest tr= taskReplyRuning.findTaskReply(request);
            if (tr != null)
            {
                TaskReply mytp = new TaskReply
                {
                    ID = tr.ID,
                    TaskInfo = tr,
                    Message = "正在执行"
                };
                return mytp;
            }
            TaskReply mytp2 = new TaskReply
            {
                ID = request.ID,
                TaskInfo = request,
                Message = "不在执行列表中"
            };
            return mytp2;
        }
        static SdtpsRPCSDK.GRPCClient mygrpc = new SdtpsRPCSDK.GRPCClient();
        /// <summary>
        /// 回复中心节点
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static TaskReply ReplyNode(TaskReply request)
        {
            string ip=   ConfigurationManager.AppSettings["centerhandleip"];
            string port = ConfigurationManager.AppSettings["centerhandleport"]; 

            mygrpc.Servadd = string.Format("{0}:{1}",ip,port);
            int errnum = 0;
            bool iserr = false;
            do
            {
                try
                {
                    iserr = false;
                    mygrpc.ReplyNode(request);

                }
                catch
                {
                    iserr = true;
                    errnum++;
                    System.Threading.Thread.Sleep(3000);//3秒后重试
                }
            }
            while (iserr && errnum < 3);//做3次重试
            TaskReply tr;
            if (iserr)
            {
                //加入到失败队列中
                taskReplyErrs.Enqueue(request);
                tr = new TaskReply();
                tr.ID = request.ID;
                tr.TaskInfo = request.TaskInfo;
                tr.Message = "到中心节点反馈失败，加到失败队列";
                tr.Status = 3;
                return tr;
            }
            tr = new TaskReply();
            tr.ID = request.ID;
            tr.TaskInfo = request.TaskInfo;
            tr.Message = "成功";
            tr.Status = 2;
            return tr;
        } 
    }
}
